#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"
#include<QDebug>
#include"mqtthandler.h"
const QString url = "http://172.21.255.1:8080/?action=stream";
HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);
    setFixedSize(800,840);

    //实例化四个界面
    m0Wig = new M0Wig;
    modbusWig = new ModbusWig;
    mbappWig = new MbappWig;
    look_ui = new monitor_ui(url);
    this->setStyleSheet("background-color:rgb(224, 224, 224)");
    this->setWindowTitle("室内环境检测");

    //将四个界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(m0Wig);
    ui->stackedWidget->addWidget(modbusWig);
    ui->stackedWidget->addWidget(mbappWig);
    ui->stackedWidget->addWidget(look_ui);


    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("172.21.255.220"));
    mqtt->setPort(1883);
    mqtt->connectToHost();

    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     m0Wig, SLOT(rece_mqtt(QByteArray)));

    connect(m0Wig,&M0Wig::close_ui_Signal,this,&HomePage::close);//接收由mowig页面点击关闭按钮从而发来的关闭页面信号

    connect(modbusWig,&ModbusWig::close_open_Signal,this,&HomePage::push_message);
    connect(m0Wig,&M0Wig::mode_op_Signal,this,&HomePage::push_message);
}

HomePage::~HomePage()
{
    delete ui;
}

void HomePage::push_message(QString type)     //发送风扇请求关闭的数据回stm32的槽函数
{
    QString json_string;
    if(type == "close_fan")
    {
        json_string = "{\"fan\":0}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);   //"home/control"
        GlobalVal::mqtt->publish(message1);
        qDebug() << "message1" << message1.topic();
    }
    else if(type == "open_fan")
    {
        json_string = "{\"fan\":1}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
        qDebug() << "message1" << message1.topic();
    }
    else if(type == "open_buzzer")
    {
        json_string = "{\"buzzer\":1}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "close_buzzer")
    {
        json_string = "{\"buzzer\":0}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "open_led1")
    {
        json_string = "{\"LED1\":1}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);  //需要发送的消息
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "close_led1")
    {
        json_string = "{\"LED1\":0}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "open_led2")
    {
        json_string = "{\"LED2\":1}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "close_led2")
    {
        json_string = "{\"LED2\":0}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "open_led3")
    {
        json_string = "{\"LED3\":1}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "close_led3")
    {
        json_string = "{\"LED3\":0}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }

    else if(type == "open_led4")
    {
        json_string = "{\"LED4\":1}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "close_led4")
    {
        json_string = "{\"LED4\":0}";
        qDebug() << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "对比模式")
    {
//        json_string = "{\"command\":\"threshold\",\"fan\":2,\"buzzer\":2,\"led1\":2,\"led2\":2,\"led3\":2,\"led4\":2}";
        json_string = "{\"command\":\"threshold\"}";
        qDebug() << "type:" << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "循环模式")
    {
        //json_string = "{\"command\":\"cycle\",\"fan\":2,\"buzzer\":2,\"led1\":2,\"led2\":2,\"led3\":2,\"led4\":2}";      //循环
        json_string = "{\"command\":\"cycle\"}";
        qDebug() << "type:" << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "刷新模式")
    {
        //json_string = "{\"command\":\"refresh\",\"fan\":2,\"buzzer\":2,\"led1\":2,\"led2\":2,\"led3\":2,\"led4\":2}";
        json_string = "{\"command\":\"refresh\"}";
        qDebug() << "type:" << type << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
    else if(type == "停止模式")
    {
        //json_string = "{\"command\":\"stop\",\"fan\":2,\"buzzer\":2,\"led1\":2,\"led2\":2,\"led3\":2,\"led4\":2}";
        json_string = "{\"command\":\"stop\"}";
        qDebug() << "type:" << json_string << endl;
        QMQTT::Message message1;
        QByteArray ba;
        ba.append(json_string);
        message1.setPayload(ba);
        message1.setTopic(TOPIC_CTL_UP);
        GlobalVal::mqtt->publish(message1);
    }
}
